package com.jianggujin.redis.util;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.MenuElement;

public class SwingUtils {
    private static Map<String, Font> fontCache = new ConcurrentHashMap<>();

    public static void resizeFont(Component component) {
        if (component instanceof MenuElement) {
            MenuElement element = (MenuElement) component;
            for (MenuElement child : element.getSubElements()) {
                resizeFont((Component) child);
            }
        }
        if (component instanceof Container) {
            Container container = (Container) component;
            for (Component child : container.getComponents()) {
                resizeFont(child);
            }
        }
        Font font = component.getFont();
        if (font != null) {
            String key = "name=" + font.getName() + ",style=" + font.getStyle();
            Font newFont = fontCache.get(key);
            if (newFont == null) {
                newFont = new Font(font.getName(), font.getStyle(), 16);
                fontCache.put(key, newFont);
            }
            component.setFont(newFont);
        }
    }
}
